import { queryCriteria, queryTaskVoucherBilltype } from '@/api/search'

export default {
  // 上下查 点击编号跳转页面触发
  beforeRouteLeave(to, form, next) {
    if (form.name) {
      this.upDownVis = false
    }
    next()
  },
  data() {
    return {
      searchSon: [], // 缺省方案传给搜索组件的数据
      loading: false, // 表格转圈
      details: [], // 搜索数据
      tacitly: 'fname', // 默认
      padlocktr: '',
      searCollData: [], // 搜索数据
      selectData: [], // 搜索下拉框数据
      tabData: [],
      header: [], // 缺省方案
      oldTabHeader: [], // 原有tableHeader
      billtypeData: [] // 单据类型数据
    }
  },
  created() {
    this.oldTabHeader = this.tableHeader
    this.queryCriteria()
  },
  methods: {
    // 搜索数据
    collect(ev) {
      this.searCollData = ev
    },
    // 获取搜索下拉框数据
    queryCriteria() {
      const DATA = {
        ftype: this.ftype,
        flanguage: this.flanguage
      }
      if (this.selectData && this.selectData.length > 0) return
      queryCriteria(DATA).then(res => {
        if (res.data && res.data.tJxSearch) {
          this.selectData = res.data.tJxSearch
        }
        this.selectData.filter(item => {
          if (item.fdefault !== 1 && this.searchSon.length === 0) {
            this.tacitly = item.fenglish
          }
          if (item.fenglish === 'billType') {
            this.taskBillType()
          }
        })
      })
    },
    // 凭证任务详情查询条件（单据类型数据）
    taskBillType() {
      queryTaskVoucherBilltype({ ftaskId: this.ftaskId }).then(res => {
        this.billtypeData = res.data.list
        this.billtypeData.unshift({ billName: '全部', billtype: 0 })
      })
    },
    // 生产模块表格 通用、连续套打报表监听切换
    forceUpdate(value, index) {
      this.$set(this.tableData[index], this.tableData[index].Fisdefault, value)
    },
    // 方案 -- 自定义列表
    customHandle(ev) {
      this.tableHeaders = []
      if (ev.header && ev.header.length > 0) {
        ev.header.forEach(item => {
          this.tableHeader1.forEach(res => {
            if (res.label === item) {
              this.tableHeaders.push(res)
            }
          })
        })
        this.tableHeader = this.tableHeaders
      } else if (ev.fname === '筛选方案') {
        this.tableHeader = this.oldTabHeader
      } else {
        this.tableHeader = this.tableHeader1
      }
    },
    // 进入物料详情
    jumpMateriel(ev) {
      const FNUMBER = ev.fnumber
      this.$router.push({ path: `/detailMateriel/${FNUMBER}` })
    },
    // 进入物料详情
    jumpMateriel1(ev) {
      this.$router.push({ path: `/detailMateriel/${ev}` })
    },
    // 自定义合计行
    getSummaries(param) {
      const { columns, data } = param
      const sums = []
      columns.forEach((column, index) => {
        const values = data.map(item => Number(item[column.property]))
        if (!values.every(value => isNaN(value))) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr)
            if (!isNaN(value)) {
              return (Number(prev) + Number(curr)).toFixed(2)
            } else {
              return prev
            }
          }, 0)
        } else {
          sums[index] = ''
        }
      })
      sums.forEach((res, index) => {
        if (Number(sums[index]) > 0) {
          sums[index] = Number(sums[index]).toLocaleString()
        } else {
          sums[index] = ''
        }
      })
      sums[0] = '合计'
      return sums
    }
  }
}
